package dbLayer;

import java.io.File;
import java.sql.*;
import java.util.Scanner;



public class DBTables implements IFDBTables {
	protected Connection con;
	public static String databaseName = "sem2project";
	
	public DBTables() {
	      con = DbConnection.getInstance().getDBcon();
	}
	
	@Override
	public int createAllTables() throws Exception {
		
		
		String query = getSqlFromFile("createTables.sql");
		int rc = -1;
		System.out.println("QUERRY : " + query);
		try {
			System.out.println("Creating database ...");
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
			System.out.println("SUCCESS: Database created!");
		}// end try
		catch (SQLException ex) {
			System.out.println("ERROR:" + ex.getMessage());
		}
		return rc;
		// TODO ALL TABLES CODE
	}

	@Override
	public int exportTables() throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int importTables(String path) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean checkTables() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int dropAllTables() throws Exception {
		String query = 
				"DROP DATABASE " + DBTables.databaseName + ";";
		int rc = -1;
		System.out.println("QUERRY : " + query);
		try {
			System.out.println("Droping all tables...");
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
			System.out.println("SUCCESS: All tables droped!");
		}// end try
		catch (SQLException ex) {
			System.out.println("ERROR:" + ex.getMessage());
		}
		return rc;
		// TODO UPDATE TABLE NAMES
	}

	@Override
	public String getSqlFromFile(String path) throws Exception {
		String content = new Scanner(new File(System.getProperty("user.dir") + "\\SQLs\\" + path)).useDelimiter("\\Z").next();
		return content;
	}
}
